package com.xbongbong.pro.statistic.service.result;

import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.service.toolbox.statistic.util.ChartResultUtil;
import com.xbongbong.pro.statistic.enums.NumFormatEnum;
import com.xbongbong.pro.statistic.enums.SystemCodeEnum;
import com.xbongbong.pro.statistic.enums.ValueTypeEnum;
import com.xbongbong.pro.statistic.result.pojo.ChartResultPojo;
import com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultInfoDTO;
import com.xbongbong.pro.statistic.result.pojo.vo.ChartResultDetailVO;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 进项发票
 *
 * @author 徐俊杰
 * @date 2020/3/16 15:05
 * @since v1.0
 */
@Component
public class JxcInvoicePurchaseResult extends ChartResultParentService implements ChartResultSystem {

    @Override
    public Object getTarget(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            case INVOICE_PURCHASE_CHART:
                return invoicePurchaseChart(chartResultPojo, chartResultDetailVO);
            default:
                break;
        }
        return null;
    }

    @Override
    public Object getValueTarget(ChartResultPojo chartResultPojo) throws XbbException {
        SystemCodeEnum systemCodeEnum = chartResultPojo.getSystemCodeEnum();
        switch (systemCodeEnum) {
            //进项开票（资金管理）
            case INVOICE_PURCHASE_HOME:
                return invoicePurchaseSingle(chartResultPojo);
            default:
                break;
        }
        return null;
    }


    /**
     * 进项发票-图表中心
     *
     * @param chartResultPojo 获取图表结果参数pojo
     * @param chartResultDetailVO
     * @return java.lang.Object
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/16 15:49
     * @since v1.0
     */
    private Object invoicePurchaseChart(ChartResultPojo chartResultPojo, ChartResultDetailVO chartResultDetailVO) throws XbbException {
        List<String> legendList = Arrays.asList("当月开票金额", "上月开票金额", "去年当月开票金额", "环比增长", "同比增长");
        List<String> unitList = Arrays.asList("元", "元", "元", "%", "%");
        StatisticResultDTO statisticResultDto = getInvoicePurchase4MomYoy(chartResultPojo, legendList, unitList);

        //右侧展示总数
        statisticResultDto.setTotalFlag(true);
        statisticResultDto.setValueTitle("开票总金额");
        chartResultDetailVO = systemChartHelp.packageResult(statisticResultDto, chartResultPojo, chartResultDetailVO);
        return chartResultDetailVO;
    }


    /**
     * 进项发票（单个值）
     *
     * @param chartResultPojo 获取图表结果参数pojo
     * @return java.lang.String
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/11 11:31
     * @since v1.0
     */
    private String invoicePurchaseSingle(ChartResultPojo chartResultPojo) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.invoicePurchaseQueryBuilder(chartResultPojo);
        String sumField = PurchaseInvoiceEnum.getEsAttr4Keyword(PurchaseInvoiceEnum.TOTAL_MONEY);
        double amount = systemChartHelp.getDouble4Total(IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE, boolQueryBuilder, sumField);
        return ChartResultUtil.formatValue2Double(false, ValueTypeEnum.VALUE_MONEY, false, amount, NumFormatEnum.getByCode(chartResultPojo.getNumFormat()), chartResultPojo.getNumFormatFlag());
    }

    /**
     * 获取并封装环比、同比统计结果
     *
     * @param chartResultPojo 参数pojo
     * @param legendList 图的legend
     * @param unitList 各条线的单位，与legendList大小一致
     * @return com.xbongbong.pro.statistic.result.pojo.dto.StatisticResultDTO
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/16 15:54
     * @since v1.0
     */
    private StatisticResultDTO getInvoicePurchase4MomYoy(ChartResultPojo chartResultPojo, List<String> legendList, List<String> unitList) throws XbbException {
        Integer startTime = chartResultPojo.getStartTime();
        Integer endTime = chartResultPojo.getEndTime();
        //环比，上月开始--截止，startTime的前一个月，作为环比的开始时间；endTime的前一个月，作为环比的截止时间
        Integer lastMonthStartTime = DateUtil.getFirstDateOfLastMonth(startTime);
        Integer lastMonthEndTime = DateUtil.getFirstDateOfLastMonth(endTime);
        //同比，上年同期（上年同一个月）开始--截止：startTime的上年同月，作为同比的开始时间；endTime的上年同月第一天，作为同比的截止时间
        Integer lastYearStartTime = DateUtil.getFirstDateOfLastYear(startTime);
        Integer lastYearEndTime = DateUtil.getFirstDateOfLastYear(endTime);

        //------当前筛选时间的结果
        ElasticAggsPojo thisMonthAggsPojo = invoicePurchase2ElasticAggsPojo(chartResultPojo, ValueTypeEnum.ARRAY_TYPE);

        //先把chartResultPojo拷贝出一份，下面都用chartResultPojoCopy，防止时间的变更对原有chartResultPojo内参数的影响
        ChartResultPojo chartResultPojoCopy = new ChartResultPojo();
        BeanUtil.copyProperties(chartResultPojo, chartResultPojoCopy);
        //------环比结果，把开始结束时间置为环比的开始结束
        chartResultPojoCopy.setStartTime(lastMonthStartTime);
        chartResultPojoCopy.setEndTime(lastMonthEndTime);
        ElasticAggsPojo lastMonthAggsPojo = invoicePurchase2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE);
        //------同比结果，把开始结束时间置为同比的开始结束
        chartResultPojoCopy.setStartTime(lastYearStartTime);
        chartResultPojoCopy.setEndTime(lastYearEndTime);
        ElasticAggsPojo lastYearAggsPojo = invoicePurchase2ElasticAggsPojo(chartResultPojoCopy, ValueTypeEnum.ARRAY_TYPE);
        //------处理环比、同比比例
        ElasticAggsPojo resultMom = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastMonthAggsPojo);
        ElasticAggsPojo resultYoy = systemChartHelp.getMomYoyResult(thisMonthAggsPojo, lastYearAggsPojo);

        List<StatisticResultInfoDTO> statisticResultInfoDtoList = systemChartHelp.getInfoDtos4MomYoy(chartResultPojo, legendList, unitList, thisMonthAggsPojo, lastMonthAggsPojo, lastYearAggsPojo, resultMom, resultYoy);
        return new StatisticResultDTO(true, thisMonthAggsPojo.getTotal(), null, statisticResultInfoDtoList);
    }

    /**
     * 获取进项发票统计结果
     *
     * @param chartResultPojo 参数pojo
     * @param valueTypeEnum 返回值类型
     * @return com.xbongbong.pro.statistic.result.pojo.ElasticAggsPojo
     * @throws XbbException 异常
     * @author 徐俊杰
     * @date 2020/3/16 15:54
     * @since v1.0
     */
    private ElasticAggsPojo invoicePurchase2ElasticAggsPojo(ChartResultPojo chartResultPojo, ValueTypeEnum valueTypeEnum) throws XbbException {
        BoolQueryBuilder boolQueryBuilder = chartQueryHelp.invoicePurchaseQueryBuilder(chartResultPojo);
        String fieldTime = PurchaseInvoiceEnum.getEsAttr4Keyword(PurchaseInvoiceEnum.INVOICE_DATE);
        String sumField = PurchaseInvoiceEnum.getEsAttr4Keyword(PurchaseInvoiceEnum.TOTAL_MONEY);
        chartResultPojo.setValueTypeEnum(ValueTypeEnum.VALUE_MONEY);
        /*--------得到进项发票金额--------*/
        return systemChartHelp.getDouble4Agg(chartResultPojo, valueTypeEnum, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE, boolQueryBuilder, fieldTime, sumField);
    }
}
